
package com.rt.schedulenew.core.support.shiro;

import com.rt.schedulebase.dto.ConfigDto;
import com.rt.schedulenew.api.IConfigService;
import com.rt.schedulenew.api.IDictUsersService;
import com.rt.schedulenew.utils.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import com.rt.schedulebase.entity.DictUsers;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.AccountException;
import com.rt.schedulebase.dto.DictUsersDto;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;

import java.util.Date;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;

import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

import org.apache.shiro.realm.AuthorizingRealm;

public class CustomRealm extends AuthorizingRealm {
    @Resource
    @Lazy
    private IDictUsersService dictUsersService;
    @Resource
    @Lazy
    private IConfigService configService;

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> stringSet = new HashSet<>();
        stringSet.add("user:show");
        stringSet.add("user:admin");
        info.setStringPermissions(stringSet);
        return null;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = authenticationToken.getPrincipal().toString();
        String userPwd = new String((char[]) authenticationToken.getCredentials());
        DictUsersDto dictUsersDto = new DictUsersDto();
        dictUsersDto.setUserId(userName);
        DictUsers dictUsers = dictUsersService.getInfo(dictUsersDto);

        if (dictUsers == null) {
            throw new AccountException("用户名或密码错误");
        }
        if (!userPwd.equals(dictUsers.getPassword())) {
            throw new AccountException("用户名或密码错误");
        }

        if (StringUtils.isNotBlank(dictUsers.getPasswordDate())) {
            ConfigDto.SystemLoginConfig loginConfig = configService.getSystemLoginConfig();
            if (Objects.nonNull(loginConfig) && Objects.nonNull(loginConfig.getLoginSet())) {
                ConfigDto.SystemLoginSet loginSet = loginConfig.getLoginSet();
                if ("1".equals(loginSet.getValidPeriod()) && StringUtils.isNotEmpty(loginSet.getDay())) {
                    try {
                        int day = Integer.parseInt(loginSet.getDay());
                        Date passwordDate = DateUtil.parse(dictUsers.getPasswordDate(), DateUtil.FORMAT_DATE);
                        // 过期日期
                        Date expireDate = DateUtil.dateAdd(passwordDate, day);
                        if (expireDate.compareTo(new Date()) <= 0) {
                            throw new AccountException("密码已过有效期");
                        }
                    } catch (NumberFormatException e) {

                    }
                }
            }
        }
        return new SimpleAuthenticationInfo(userName, userPwd, getName());
    }
}
